package org.example.quartz.controller;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.example.quartz.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(path = "/freeMap")
public class FreeMapController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value(value = "${spring.datasource.databaseType}")
    private String databaseType;
    @Value(value = "${spring.datasource.GSYY}")
    private String GSYY;

    @Value("${spring.datasource.url}")
    private String jdbcUrl;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

    /**
     * 数据查询
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> param) {
        try {
            String condition = "";
            // 拼接条件
            if (!ObjectUtils.isEmpty(param.get("doccode"))) {
                condition += " and  DOCCODE like '%" + param.get("doccode") + "%' ";

            }
            if (!ObjectUtils.isEmpty(param.get("docname"))) {
                condition += " and  DOCNAME like '%" + param.get("docname") + "%' ";

            }
            Integer count = jdbcTemplate.queryForObject("select count(*) from " + GSYY + ".freemap where DELETE_STATE = 0 " + condition, Integer.class);
            if ("oracle".equals(databaseType)) {
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select " +
                        "ID as \"id\",\n" +
                        "NUM as \"num\",\n" +
                        "DOCCODE as \"doccode\",\n" +
                        "DOCNAME as \"docname\",\n" +
                        "GSCODE as \"gscode\",\n" +
                        "TABLENAME as \"tablename\",\n" +
                        "TABLEKEY as \"tablekey\",\n" +
                        "CODEKEY as \"codekey\",\n" +
                        "NAMEKEY as \"namekey\",\n" +
                        "FZPK as \"fzpz\",\n" +
                        "FREEKEY as \"freekey\",\n" +
                        "ISTRUE as \"istrue\",\n" +
                        "TS as \"ts\"\n" +
                        " from " + GSYY + ".freemap where DELETE_STATE = 0 " + condition + "order by GSCODE desc OFFSET ((" + Integer.parseInt(param.get("pageNum").toString()) + " - 1 ) *" + Integer.parseInt(param.get("pageSize").toString()) + ") rows fetch next " + Integer.parseInt(param.get("pageSize").toString()) + " rows only");
                return Result.success(mapList, count);
            } else if ("dm".equals(databaseType)) {
                String sql = "select " +
                        "ID as \"id\",\n" +
                        "NUM as \"num\",\n" +
                        "DOCCODE as \"doccode\",\n" +
                        "DOCNAME as \"docname\",\n" +
                        "GSCODE as \"gscode\",\n" +
                        "TABLENAME as \"tablename\",\n" +
                        "TABLEKEY as \"tablekey\",\n" +
                        "CODEKEY as \"codekey\",\n" +
                        "NAMEKEY as \"namekey\",\n" +
                        "FZPK as \"fzpz\",\n" +
                        "FREEKEY as \"freekey\",\n" +
                        "ISTRUE as \"istrue\",\n" +
                        "TS as \"ts\"\n" +
                        " from (select t.*,ROWNUM rn from " + GSYY + ".freemap t where DELETE_STATE =0  " + condition + ") where rn between " + (Integer.parseInt(param.get("pageNum").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()) + " and " + (Integer.parseInt(param.get("pageNum").toString())) * Integer.parseInt(param.get("pageSize").toString());
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
                return Result.success(mapList, count);
            } else {
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from " + GSYY + ".freemap where DELETE_STATE = 0 " + condition + "limit " + Integer.parseInt(param.get("pageSize").toString()) + " OFFSET " + (Integer.parseInt(param.get("pageNum").toString()) - 1) * Integer.parseInt(param.get("pageSize").toString()));
                return Result.success(mapList, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @PostMapping("/getById")
    public Result getById(@RequestParam("id") Integer id) {
        try {
            Map<String, Object> map = jdbcTemplate.queryForMap("select * from " + GSYY + ".freemap where id = ?", id);
            return Result.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @PostMapping("/add")
    public Result addFreeMap(@RequestBody Map<String, Object> param) {
        try {
            if ("oracle".equals(databaseType) || "dm".equals(databaseType)) {
                String insertSql = "INSERT INTO " + GSYY + ".\"FREEMAP\" ( \"ID\", \"NUM\", \"DOCCODE\", \"DOCNAME\", \"GSCODE\", \"TABLENAME\", \"TABLEKEY\", \"CODEKEY\",\"NAMEKEY\",\"FZPK\",\"FREEKEY\",\"ISTRUE\",\"TS\" )\n" +
                        "VALUES\n" +
                        "\t( \n" +
                        "\t'" + String.valueOf(IdUtil.getSnowflakeNextId()).substring(11) + "', \n" +
                        "\t'" + param.get("num") + "', \n" +
                        "\t'" + param.get("doccode") + "', \n" +
                        "\t'" + param.get("docname") + "', \n" +
                        "\t'" + param.get("gscode") + "', \n" +
                        "\t'" + param.get("tablename") + "', \n" +
                        "\t'" + param.get("tablekey") + "', \n" +
                        "\t'" + param.get("codekey") + "', \n" +
                        "\t'" + param.get("namekey") + "', \n" +
                        "\t'" + param.get("fzpk") + "', \n" +
                        "\t'" + param.get("freekey") + "', \n" +
                        "\t'" + param.get("istrue") + "', \n" +
                        "to_date('1900-01-31 00:00:00', 'yyyy-mm-dd hh24:mi:ss')" +
                        "\t)";
                jdbcTemplate.update(insertSql);
                return Result.success("新增成功");
            } else {
                String insertSql = "INSERT INTO " + GSYY + ".FREEMAP ( ID, NUM, DOCCODE, DOCNAME, GSCODE, TABLENAME, TABLEKEY, CODEKEY,NAMEKEY,FZPK,FREEKEY,ISTRUE,TS )\n" +
                        "VALUES\n" +
                        "\t( \n" +
                        "\t'" + String.valueOf(IdUtil.getSnowflakeNextId()).substring(11) + "', \n" +
                        "\t'" + param.get("num") + "', \n" +
                        "\t'" + param.get("doccode") + "', \n" +
                        "\t'" + param.get("docname") + "', \n" +
                        "\t'" + param.get("gscode") + "', \n" +
                        "\t'" + param.get("tablename") + "', \n" +
                        "\t'" + param.get("tablekey") + "', \n" +
                        "\t'" + param.get("codekey") + "', \n" +
                        "\t'" + param.get("namekey") + "', \n" +
                        "\t'" + param.get("fzpk") + "', \n" +
                        "\t'" + param.get("freekey") + "', \n" +
                        "\t'" + param.get("istrue") + "', \n" +
                        "\t'" + "1900-01-31 00:00:00" + "' \n" +
                        "\t)";
                jdbcTemplate.update(insertSql);
                return Result.success("新增成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("新增失败！！！");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result deleteFreeMap(@RequestParam("id") String id) {
        try {
            //如果ID为空不能删除
            if (StringUtils.isEmpty(id)) {
                return Result.fail("请选择要删除的数据！！！");
            }
            String deleteSql = "UPDATE " + GSYY + ".FREEMAP SET DELETE_STATE = 1  WHERE ID = '" + id + "'";
            int update = jdbcTemplate.update(deleteSql);
            String msg = "";
            if (update > 0) {
                msg = "删除成功";
                return Result.success(msg);
            } else {
                msg = "删除失败";
                return Result.fail(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 修改
     *
     * @param param
     * @return
     */
    @PostMapping("/update")
    public Result updateFreeMap(@RequestBody Map<String, Object> param) {
        try {
            if ("oracle".equals(databaseType) || "dm".equals(databaseType)) {
                String updateSql = "UPDATE " + GSYY + ".\"FREEMAP\" \n" +
                        "SET \"NUM\" = '" + param.get("num") + "',\n" +
                        "\"DOCCODE\" = '" + param.get("doccode") + "',\n" +
                        "\"DOCNAME\" = '" + param.get("docname") + "',\n" +
                        "\"GSCODE\" = '" + param.get("gscode") + "',\n" +
                        "\"TABLENAME\" = '" + param.get("tablename") + "',\n" +
                        "\"TABLEKEY\" = '" + param.get("tablekey") + "',\n" +
                        "\"CODEKEY\" = '" + param.get("codekey") + "',\n" +
                        "\"NAMEKEY\" = '" + param.get("namekey") + "',\n" +
                        "\"FZPK\" = '" + param.get("fzpk") + "',\n" +
                        "\"FREEKEY\" = '" + param.get("freekey") + "',\n" +
                        "\"ISTRUE\" = '" + param.get("istrue") + "'\n" +
                        "WHERE\n" +
                        "\t\"ID\" = '" + param.get("id") + "'";
                jdbcTemplate.update(updateSql);
            } else {
                String updateSql = "UPDATE " + GSYY + ".FREEMAP \n" +
                        "SET NUM = '" + param.get("num") + "',\n" +
                        "DOCCODE = '" + param.get("doccode") + "',\n" +
                        "DOCNAME = '" + param.get("docname") + "',\n" +
                        "GSCODE = '" + param.get("gscode") + "',\n" +
                        "TABLENAME = '" + param.get("tablename") + "',\n" +
                        "TABLEKEY = '" + param.get("tablekey") + "',\n" +
                        "CODEKEY = '" + param.get("codekey") + "',\n" +
                        "NAMEKEY = '" + param.get("namekey") + "',\n" +
                        "FZPK = '" + param.get("fzpk") + "',\n" +
                        "FREEKEY = '" + param.get("freekey") + "',\n" +
                        "ISTRUE = '" + param.get("istrue") + "'\n" +
                        "WHERE\n" +
                        "\tID = '" + param.get("id") + "';";
                jdbcTemplate.update(updateSql);
            }

            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }
}
